package com.jec.module.business.entity;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jec.utils.Constants;
import com.jec.utils.DateTimeUtils;
import com.jec.utils.XmlUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * Created by jeremyliu on 6/19/16.
 */
@Entity
@Table(name="zhwg_record")
public class Record implements Serializable{

    /**
	 * 
	 */
	private static final long serialVersionUID = -4843305483475472545L;

	@Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment",strategy="increment")
	@Getter @Setter
    private int id;

    @Column(name="calling_number")
    @Getter @Setter
    private String callingNumber;

    @Column(name="called_number")
    @Getter @Setter
    private String calledNumber;

    @Column(name="start_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @Getter @Setter
    private Date startTime;

    @Column(name="period_time")
    @Getter @Setter
    private long period;

    @Column(name="file")
    private String filePath;

    @Transient
    private List<RecordSegment> segments = new ArrayList<>();


    @JsonIgnore
    public String getFilePath() {
        return filePath;
    }

    @JsonIgnore
    public String getPcmFile(){
        return filePath + File.separator + "data";
    }

    @JsonIgnore
    public String getXmlFile(){
        return filePath + File.separator + "info.xml";
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public void setFilePath(int id){
        String date = DateTimeUtils.Date2String(new Date());
        filePath = Constants.recordPath + File.separator + date + File.separator + id;
        File file = new File(filePath);
        if(!file.exists() || !file.isDirectory())
            file.mkdirs();
    }


    @JsonIgnore
    public List<RecordSegment> getSegments() {
        return segments;
    }

    public void setSegments(List<RecordSegment> segments) {
        this.segments = segments;
    }

    public void readConfig(){
        segments = XmlUtils.readXml(getXmlFile(),RecordSegment.class);
    }
}
